package org.jetnuke.modules.blog;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BlogCommentDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.BlogComment;
import org.jetnuke.entities.BlogPost;
import org.jetnuke.util.JetUtil;
import org.jetnuke.util.StringUtils;

public class UpdateComment implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UpdateComment.class);

	int id;
	String approved;
	private String author;
	private String email;
	private String author_url;
	private String content;

	public void setId(int id) {
		this.id = id;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		BlogCommentDAO bcm = DataAccessDriver.getInst().newBlogCommentDAO();
		BlogComment bc = bcm.retrieveByPK(id);
		int post_id = bc.getPOST_ID();
		BlogPost bp = DataAccessDriver.getInst().newBlogPostDAO().retrieveByPK(
				post_id);
		int aid = bp.getAID();
		if (ui == null || ui.getID() != aid)
			return null;
		bc.setAPPROVED(approved);
		bc.setAUTHOR(author);
		bc.setAUTHOR_EMAIL(email);
		bc.setAUTHOR_URL(author_url);
		bc.setCONTENT(content);

		bcm.update(bc);
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/blog/show_page?id=" + post_id);

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setApproved(String approved) {
		this.approved = approved;
	}

	public void setAuthor(String author) {
		if (logger.isDebugEnabled()) {
			logger.debug("setAuthor(String) - start"); //$NON-NLS-1$
		}

		this.author = StringUtils.filterText(author, true);
		;

		if (logger.isDebugEnabled()) {
			logger.debug("setAuthor(String) - end"); //$NON-NLS-1$
		}
	}

	public void setAuthor_url(String author_url) {
		if (logger.isDebugEnabled()) {
			logger.debug("setAuthor_url(String) - start"); //$NON-NLS-1$
		}

		this.author_url = StringUtils.filterText(author_url, true);

		if (logger.isDebugEnabled()) {
			logger.debug("setAuthor_url(String) - end"); //$NON-NLS-1$
		}
	}

	public void setContent(String content) {
		if (logger.isDebugEnabled()) {
			logger.debug("setContent(String) - start"); //$NON-NLS-1$
		}

		this.content = StringUtils.filterText(content, false);

		if (logger.isDebugEnabled()) {
			logger.debug("setContent(String) - end"); //$NON-NLS-1$
		}
	}

	public void setEmail(String email) {
		if (logger.isDebugEnabled()) {
			logger.debug("setEmail(String) - start"); //$NON-NLS-1$
		}

		this.email = StringUtils.filterText(email, true);

		if (logger.isDebugEnabled()) {
			logger.debug("setEmail(String) - end"); //$NON-NLS-1$
		}
	}

}
